const utils = require("../../../utils/utils")
const app = getApp();
// pages/print/specification/specification.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        detail: [],
        code: '',
        pic: '',
        pdbottom: 0,
        name: ''
    },
    btnConfirm() {
        var that = this;
        var code = that.data.code;
        wx.showActionSheet({
            itemList: ['相册', '拍照', '从QQ会话选择'],
            success(res) {
                if (res.tapIndex == 0) {
                    that.getPhoto('album', code);
                }
                if (res.tapIndex == 1) {
                    that.getPhoto('camera', code);

                }
                if (res.tapIndex == 2) {
                    that.getPhoto1(code);
                }
            },
            fail(res) {

            }
        })
    },
    //拍照调取相册
    getPhoto(type, code) {
        var that = this;
        wx.chooseImage({
      count: 1,
      sizeType: ['original'],
      sourceType: [type],
            camera: 'front',
            success(res) {
                const tempFilePaths = res.tempFilePaths[0];
                that.selectComponent("#loading").showLoadingCus();
                that.uploadImgFile(tempFilePaths)
                // wx.redirectTo({
                //     url: '../photoEdit/photoEdit?code=' + code + '&w=300&h=200&img=' + tempFilePaths+'&picType='+that.data.name
                //   })
            }
        })
    },
    getPhoto1(code) {
        var that = this;
        wx.chooseMessageFile({
            count: 1,
            type: 'image',
            success(res) {
                that.selectComponent("#loading").showLoadingCus();
                that.uploadImgFile(res.tempFiles[0].path)
                // wx.redirectTo({
                //     url: '../photoEdit/photoEdit?code=' + code + '&w=300&h=200&img=' + res.tempFiles[0].path+'&picType='+that.data.name
                // })
            }
        })
    },
    //上传图片
    uploadImgFile(path) {
        var that = this;
        wx.getImageInfo({
            src: path,
            success(res) {
                var tempWidth = res.width;
                var tempHeight = res.height;
                var tempPath = res.path;
                if (tempHeight > 2000 || tempWidth > 2000) {
                    qq.compressImage({
                        src:tempPath, // 图片路径
                        destHeight:tempHeight>2000?2000:tempHeight,
                        destWidth:tempWidth>2000?2000:tempWidth,
                        success(res){
                            utils.uploadImg(res.tempFilePath, function (res) {
                                that.selectComponent("#loading").hideLoadingCus();
                                var data = JSON.parse(res.data).data
                                wx.navigateTo({
                                    url: '../papersEdit/papersEdit?&url=' + data.fileUrl + '&code=' + that.data.code + '&serviceSubtypeId=' + that.data.serviceSubtypeId + '&serviceMainId=' + that.data.serviceMainId + '&tagFloor=' + that.data.tagFloor + '&title=' + that.data.name
                                })
                            })
                        },
                        fail(err){
                           
                        }
                      })
                } else {
                    utils.uploadImg(path, function (res) {
                        that.selectComponent("#loading").hideLoadingCus();
                        var data = JSON.parse(res.data).data
                        wx.navigateTo({
                            url: '../papersEdit/papersEdit?&url=' + data.fileUrl + '&code=' + that.data.code + '&serviceSubtypeId=' + that.data.serviceSubtypeId + '&serviceMainId=' + that.data.serviceMainId + '&tagFloor=' + that.data.tagFloor + '&title=' + that.data.name
                        })
                    })
                }

            }
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var that = this;
        qq.setNavigationBarTitle({
            title: options.name
        })
        that.setData({
            code: options.code,
            serviceSubtypeId: options.serviceSubtypeId,
            serviceMainId: options.serviceMainId,
            tagFloor: options.tagFloor,
            name: options.name
        })
        if (utils.is_define(options.detail)) {
            var str = JSON.parse(options.detail);
            var array = str.split('\n')
            that.setData({
                detail: array
            })
        }
        wx.getSystemInfo({
            success: (result) => {
                var model = result.model;
                if (model == 'iPhone X') {
                    that.setData({
                        pdbottom: '68'
                    })
                } else {
                    that.setData({
                        pdbottom: '40'
                    })
                }
            },
        })
        that.setData({
            pic: options.pic,
            code: options.code
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    }
})